package com.yootk.service.advice;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
@Aspect
@Configuration
public class ServiceAdvice {
    private static final Logger LOGGER = LoggerFactory.getLogger(ServiceAdvice.class) ;
    @Before(argNames = "msg", value = "execution(public * com.yootk..service..*.*(..)) && args(msg)")
    public void beforeHandle(String msg) {
        LOGGER.info("启用业务功能前置调用处理机制，方法参数：{}", msg);
        if (!msg.contains("sun")){
            throw new RuntimeException("你是一个笨蛋吗") ;
        }
    }
    @After(argNames = "msg",value = "execution(public * com.yootk..service..*.*(..)) && args(msg)")
    public void afterHandle(String msg) {
        LOGGER.info("启用业务功能后置调用处理机制，方法参数：{}", msg);
    }
    @Pointcut("execution(public * com.yootk..service..*.*(..))")
    public void pointcut(){}
    @AfterReturning(pointcut = "pointcut()" ,returning = "msg",argNames = "msg")
    public void afterReturningHandle(String msg) { // 后置返回业务通知
        LOGGER.info("启用业务功能后置返回处理机制，方法参数：{}", msg);
    }
    @AfterThrowing(pointcut = "pointcut()",throwing = "e",argNames = "e")
    public void afterThrowHandle(Exception e) { // 后置返回业务通知
        LOGGER.info("启用业务功能后置异常处理机制，异常信息：{}", e.getMessage());
    }
    @Around("pointcut()")
    public Object handleRound(ProceedingJoinPoint point) throws Throwable{

        LOGGER.info("around前置处理前的接收的参数：{}", Arrays.toString(point.getArgs()));
        Object returnValue = null ;
        try {
            returnValue = point.proceed(point.getArgs()) ;
        } catch (Exception e){
            LOGGER.info("around异常产生：{}",e.getMessage());
            throw e ;
        }
        LOGGER.info("around后置接受参数：{}",returnValue);
        return returnValue ;
    }
}
